package ui;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Arrays;

import service.*;
import model.*;
import util.*;

import javax.swing.JFrame;

public class Register extends JFrame {
	private UserService userService=UserService.getInstance();
	private Toolkit toolkit= Toolkit.getDefaultToolkit();
	private Dimension sc=toolkit.getScreenSize();
	private int loc1=600;
	private int loc2=800;

	
	private JLabel title=new JLabel("用户注册");
	private JLabel uid=new JLabel("账号");
	private JLabel pass=new JLabel("密码");
	private JLabel name=new JLabel("姓名");
	private JLabel sex=new JLabel("性别");
	private JLabel age=new JLabel("年龄");
	private JLabel nationality=new JLabel("国籍");
	private JLabel fromCountry=new JLabel("来源国");
	private JLabel id=new JLabel("证件号");
	private JLabel pass2=new JLabel("确认密码");
	
	private JTextField tuid=new JTextField(20);
	private JPasswordField tpass=new JPasswordField(20);
	private JTextField tname=new JTextField(20);
	private JTextField tsex=new JTextField(20);
	private JTextField tage=new JTextField(20);
	private JTextField tnationality=new JTextField(20);
	private JTextField tfromCountry=new JTextField(20);
	private JTextField tid=new JTextField(20);
	private JPasswordField tpass2=new JPasswordField(20);
	
	private JButton button1=new JButton("注册");
	private JButton button2=new JButton("返回");
	
	private Font font1 = new Font("华文中宋", Font.PLAIN, 40);
    private Font font2 = new Font("华文新魏", Font.PLAIN, 25); 
	public Register() {
		setSize(loc1,loc2);
		setLayout(new GridLayout(11,1));
		setLocation((sc.width-loc1)/2,(sc.height-loc2)/2);
		
		JPanel[] p= {new JPanel(),new JPanel(),new JPanel(),new JPanel(),new JPanel(),
				new JPanel(),new JPanel(),new JPanel(),new JPanel(),new JPanel(),new JPanel()};
		
		title.setFont(font1);
		pass.setFont(font2);
		tpass.setEchoChar('*');
		uid.setFont(font2);
		name.setFont(font2);
		age.setFont(font2);
		sex.setFont(font2);
		nationality.setFont(font2);
		fromCountry.setFont(font2);
		pass2.setFont(font2);
		tpass2.setEchoChar('*');
		id.setFont(font2);
		button1.setFont(font2);
		button2.setFont(font2);
		
		p[0].add(title);
		p[1].add(uid);
		p[1].add(tuid);
		p[2].add(name);
		p[2].add(tname);
		p[3].add(age);
		p[3].add(tage);
		p[4].add(sex);
		p[4].add(tsex);
		p[5].add(nationality);
		p[5].add(tnationality);
		p[6].add(fromCountry);
		p[6].add(tfromCountry);
		p[8].add(pass);
		p[8].add(tpass);
		p[9].add(pass2);
		p[9].add(tpass2);
		p[7].add(id);
		p[7].add(tid);
		p[10].setLayout(new FlowLayout(FlowLayout.CENTER,150,0));
		p[10].add(button1);
		p[10].add(button2);
		
		for(int i=0;i<11;i++) {
           p[i].setBackground(Color.cyan);
			add(p[i]);
		}
		//返回按钮添加监听器
		 button2.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				tuid.setText(null);
				tpass.setText(null);
				tpass2.setText(null);
				tage.setText(null);
				tsex.setText(null);
				tid.setText(null);
				tnationality.setText(null);
				tfromCountry.setText(null);
				tname.setText(null);
				setVisible(false);
				Login.loginShow();
			 }
		 });
		 
		 //注册按钮添加监听事件
		 button1.addActionListener(new ActionListener(){
			 
 
			 public void actionPerformed(ActionEvent e) {
				 
				 String pass=new String(tpass.getPassword());
				 String pass2=new String(tpass2.getPassword());
				 String name=tname.getText();
				 String sex=tsex.getText();
				 String n=tnationality.getText();
				 String f=tfromCountry.getText();
				 String id=tid.getText();
				 
				 
				 if(tuid.getText().equals("")||pass.equals("")||pass2.equals("")||name.equals("")
						 ||sex.equals("")||n.equals("")||f.equals("")||tage.getText().equals("")||id.equals("")) {
					 ShowMessage.show("有项目未填！\n请填写完整！");
				 }else {
				 int uid=0;
				  try {
                    // String型id转int型id，不可转（非数字字符）时会抛异常
					  uid=Integer.parseInt(tuid.getText()); 
                } catch (Exception e1) {
                    e1.printStackTrace();
                    ShowMessage.show("账号格式错误！\n请输入纯数字账号！");
                }
				  
				  int age=0;
				  try {
	                    
					   age=Integer.parseInt(tage.getText()); 
	                } catch (Exception e2) {
	                    e2.printStackTrace();
	                    ShowMessage.show("输入年龄格式错误！\n请输入纯数字！");
	                }
				
					 if(!pass.equals(pass2)) {
						 ShowMessage.show("两次填写的密码不一致！");
					 }else {
						 userService.addUser(uid, pass, name, sex, age, n, f, id);
					 }
				 }
				 
			 }
		 });
		
	}
	
	

	public static void registerShow(){
		JFrame r=new Register();
		r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		r.setTitle("境外人员酒店隔离管理系统");
		r.setVisible(true);
	}
}
